package com.itheima.dbsharding.month.algorithm;

import com.itheima.dbsharding.month.tool.DateUtils;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Date;

/**
 * @author jiangta
 * @version 1.0
 * @date 2020/5/8 11:42 上午
 */
public class MonthShardingAlgorithm implements PreciseShardingAlgorithm<Date> {

    /**
     * Date 是分片列对应的java类型
     *
     * @param collection
     * @param preciseShardingValue
     * @return
     */
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {

        System.out.println("collection:" + collection + " PreciseShardingValue:"+ preciseShardingValue);
        Date date = preciseShardingValue.getValue();
        LocalDateTime localDateTime = DateUtils.UDateToLocalDateTime(date);
        StringBuffer tableName = new StringBuffer();
        tableName.append(preciseShardingValue.getLogicTableName()).append("_")
                .append(localDateTime.format(DateTimeFormatter.ofPattern("yyyyMM")));
        return tableName.toString();
    }
}
